#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_20_clicked()
{
    ui->plainTextEdit->appendPlainText("\n===front(), back()函数测试");

    QString str1=ui->comboBox1->currentText();
    ui->plainTextEdit->appendPlainText("str1= "+str1);

    QChar  ch=str1.front();
    QString str2=QString(ch);
    ui->plainTextEdit->appendPlainText("front()= "+str2);

    ch=str1.back();
    str2=QString(ch);
    ui->plainTextEdit->appendPlainText("back()= "+str2);
}


void Widget::on_pushButton_15_clicked()
{
    QString str1=ui->comboBox1->currentText();
    ui->plainTextEdit->appendPlainText("str1= "+str1);

    QString str2=str1.left(2);
    ui->plainTextEdit->appendPlainText("left(2)= "+str2);

    str2=str1.right(6);
    ui->plainTextEdit->appendPlainText("right(6)= "+str2);
}


void Widget::on_pushButton_23_clicked()
{
    ui->plainTextEdit->appendPlainText("\n===first(), last()函数测试");

    QString str1=ui->comboBox1->currentText();  //获取str1输入
    QString str2=ui->comboBox2->currentText();  //获取str2输入
    ui->plainTextEdit->appendPlainText("str1= " +str1);
    ui->plainTextEdit->appendPlainText("str2= " +str2);

    int N= str1.lastIndexOf(str2);    //str2出现的最后位置
    QString str3= str1.first(N+1);    //路径名称,带最后的"\"
    ui->plainTextEdit->appendPlainText(str3);

    str3= str1.last(str1.size()-N-1);   //去除路径的文件名
    ui->plainTextEdit->appendPlainText(str3);
}

void Widget::on_pushButton_14_clicked()
{
    ui->plainTextEdit->appendPlainText("\n ===section 函数测试");

    QString str1=ui->comboBox1->currentText();  //获取str1输入
    QString str2=ui->comboBox2->currentText();  //获取str2输入
    ui->plainTextEdit->appendPlainText("str1= " +str1);
    ui->plainTextEdit->appendPlainText("str2= " +str2);


    int N= ui->spinBox->value();    //str2出现的最后位置
    QString str = str1.section(str2, N);
    ui->plainTextEdit->appendPlainText(str);
}

void Widget::ShowBoolInfo(QString str, bool ret)
{
    if(ret)
        ui->plainTextEdit->appendPlainText(str + "ret = true");
    else
        ui->plainTextEdit->appendPlainText(str + "ret = false");
}

void Widget::on_pushButton_18_clicked()
{
    QString str1=ui->comboBox1->currentText();
    QString str2=ui->comboBox2->currentText();
    ShowBoolInfo(str1, str1.isNull() || str1.isEmpty());
    ShowBoolInfo(str2, str2.isNull() || str2.isEmpty());
}


void Widget::on_pushButton_26_clicked()
{
    ui->plainTextEdit->appendPlainText("\n===resize()函数测试");

    QString str1;
    str1.resize(5,'0');       //str1="00000"
    ui->plainTextEdit->appendPlainText(str1);

    str1.resize(10,QChar(0x54C8));   //str1="00000哈哈哈哈哈", 0x54C8是‘哈’的Unicode编码
    ui->plainTextEdit->appendPlainText(str1);
}



void Widget::on_pushButton_2_clicked()
{
    ui->plainTextEdit->appendPlainText("\n===resize()函数测试");

    QString str1;
    str1.fill('s', 5);       //str1="00000"
    ui->plainTextEdit->appendPlainText(str1);

    str1.fill('x');   //str1="00000哈哈哈哈哈", 0x54C8是‘哈’的Unicode编码
    ui->plainTextEdit->appendPlainText(str1);
}


void Widget::on_pushButton_7_clicked()
{
    QString str1 = "1,\2,3.4k5\\6[]23km\\555\\";
    QString str2 = "\\";
    ui->plainTextEdit->appendPlainText("str1= " +str1);
    ui->plainTextEdit->appendPlainText("str2= " +str2);

    int index1 = str1.indexOf(str2);
    int index2 = str1.lastIndexOf(str2);
    ui->plainTextEdit->appendPlainText("index :" + QString::number(index1) + "last index :" + QString::number(index2));
}


void Widget::on_pushButton_3_clicked()
{

    QString str1=ui->comboBox1->currentText();
    QString str2=ui->comboBox2->currentText();
    ShowBoolInfo(str1, str1.contains(str2));
}

void Widget::on_pushButton_6_clicked()
{
    QString str1=ui->comboBox1->currentText();
    QString str2=ui->comboBox2->currentText();
    ShowBoolInfo(str1, str1.endsWith(str2) || str1.startsWith(str2));
}

void Widget::on_pushButton_22_clicked()
{
    QString str1=ui->comboBox1->currentText();
    QString str2=ui->comboBox2->currentText();
    int count = str1.count(str2);
    ui->plainTextEdit->appendPlainText("count :" + QString::number(count));
}

void Widget::on_pushButton_13_clicked()
{
    QString str1="  Are  you  Ok  ?  ";
    QString str = str1.trimmed();
    ui->plainTextEdit->appendPlainText(str);
    str = str1.simplified();
    ui->plainTextEdit->appendPlainText(str);
}

void Widget::on_pushButton_clicked()
{
    QString str1=ui->comboBox1->currentText();
    QString str2=ui->comboBox2->currentText();
    str1 = str1.insert(0, "A");
    str1 = str1.insert(str1.size() - 1, "B");
    str1 = str1.insert(str1.size(), "E");
    ui->plainTextEdit->appendPlainText(str1);
}

void Widget::on_pushButton_28_clicked()
{
    QString str1 = "1,\2,3.4k5\\6[]23km\\555\\";
    QString str2 = "\\";
    str1 = str1.remove(str2);
    ui->plainTextEdit->appendPlainText(str1);
}

void Widget::on_pushButton_19_clicked()
{
    QString str = "It is great";
    int N = str.lastIndexOf(" ");
    QString subStr = "good";
    str.replace(N + 1, subStr.size() + 1, subStr);
    ui->plainTextEdit->appendPlainText(str);
    str.replace(N + 1, subStr.size() + 1, "ok");
    ui->plainTextEdit->appendPlainText(str);
    str = "oooooookkkkkk";
    str.replace('o', 'e');
    ui->plainTextEdit->appendPlainText(str);
}

void Widget::on_pushButton_4_clicked()
{
    QString str1=ui->comboBox1->currentText();
    QString str2=ui->comboBox2->currentText();

    ui->plainTextEdit->appendPlainText(QString::number(str1.size()));
    ui->plainTextEdit->appendPlainText(QString::number(str2.length()));
}


void Widget::on_btnClearText_clicked()
{
    ui->plainTextEdit->clear();
}

